package com.mb.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.dao.TbProductShipFeeCfgMapper;
import com.mb.app.domain.SysAreaCoord;
import com.mb.app.domain.TbProductShipFeeCfg;
import com.mb.app.service.ISysAreaCoordService;
import com.mb.app.service.ITbProductShipFeeCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品运费配置  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-02
 */
@Service
public class TbProductShipFeeCfgServiceImpl extends ServiceImpl<TbProductShipFeeCfgMapper, TbProductShipFeeCfg> implements ITbProductShipFeeCfgService {

    @Autowired
    private ISysAreaCoordService sysAreaCoordService;

    @Override
    public List<TbProductShipFeeCfg> getProductShipList(Map<String, Object> map) {
        List<TbProductShipFeeCfg> tbProductShipFeeCfgs = this.baseMapper.selectByMap1(map);
        List<TbProductShipFeeCfg> tbProductShipFeeCfgs1 = new ArrayList<TbProductShipFeeCfg>();
        if(tbProductShipFeeCfgs.size()>0) {
            for(int i = 0; i < tbProductShipFeeCfgs.size();i++) {
                TbProductShipFeeCfg productShipFeeCfg = tbProductShipFeeCfgs.get(i);
                if(productShipFeeCfg != null && productShipFeeCfg.getAreaIds() != null && productShipFeeCfg.getAreaIds() != "") {
                    String[] areaIds = productShipFeeCfg.getAreaIds().split(",");
                    String sysName = "";
                    String sysIds = "";
                    for (int j = 0; j < areaIds.length; j++){
                        SysAreaCoord sysAreaCoord = sysAreaCoordService.selectById(areaIds[j]);
                        if (sysAreaCoord != null) {
                            sysName = sysName + "," + sysAreaCoord.getName();
                            sysIds = sysIds + "," + sysAreaCoord.getId();
                        }
                    }
                    if (sysName != "" && sysIds != "") {
                        sysName = sysName.substring(1);
                        sysIds = sysIds.substring(1);
                    }
                    productShipFeeCfg.setAreaIds(sysIds);
                    productShipFeeCfg.setAreaNames(sysName);
                }
                tbProductShipFeeCfgs1.add(productShipFeeCfg);
            }
        }
        return tbProductShipFeeCfgs1;
    }

    @Override
    public int save(TbProductShipFeeCfg tbProductShipFeeCfg, Long userId) {
        int  istre = 0;
        if (tbProductShipFeeCfg.getId() != null && tbProductShipFeeCfg.getId() != 0) {
            // 修改
             istre = this.baseMapper.updateById(tbProductShipFeeCfg);
        }else {
            // 添加
            istre = this.baseMapper.insert(tbProductShipFeeCfg);
        }
        return istre;
    }

    @Override
    public int deleteShipFee(List<TbProductShipFeeCfg> tbProductShipFeeCfgs, Long userId) {
        if (tbProductShipFeeCfgs != null && tbProductShipFeeCfgs.size()> 0) {
            for (int i = 0; i < tbProductShipFeeCfgs.size(); i++) {
                TbProductShipFeeCfg tbProductShipFeeCfg = tbProductShipFeeCfgs.get(i);
                TbProductShipFeeCfg tbProductShipFeeCfg1 = new TbProductShipFeeCfg();
                tbProductShipFeeCfg1.setId((tbProductShipFeeCfg.getId()));
                tbProductShipFeeCfg1.setDataStatus(0);
                this.baseMapper.updateById(tbProductShipFeeCfg1);
            }
        }
        return 1;
    }
}
